//
//  XS_InviteVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/18.
//

import UIKit

class XS_InviteVC: XS_BaseVC {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    
    @IBOutlet weak var sessionBtn: QMUIButton!
    @IBOutlet weak var timelineBtn: QMUIButton!
    @IBOutlet weak var qqBtn: QMUIButton!
    @IBOutlet weak var picBtn: QMUIButton!
    
    @IBOutlet weak var snapView: UIView!
    @IBOutlet weak var codeImageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "邀请好友一起学习"
        scrollView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        sessionBtn.imagePosition = .top
        sessionBtn.spacingBetweenImageAndTitle = 8
        timelineBtn.imagePosition = .top
        timelineBtn.spacingBetweenImageAndTitle = 8
        picBtn.imagePosition = .top
        picBtn.spacingBetweenImageAndTitle = 8
        
        XS_NetWork.requestJson("/setting/querySettings")
            .mapModel(type: XS_SettingModel.self) // 返回 模型对象 和原始数据
            .subscribe(onNext: { setModel, resp in
                DispatchQueue.global().async {
                    let context = CIContext()
                    let data = ((setModel?.online_url ?? "")+"/login?sourceType=friend&sourceId="+String(XS_Singleton.shared.infoModel?.userBO.id ?? 0)).data(using: String.Encoding.utf8)
                    if let filter = CIFilter(name: "CIQRCodeGenerator") {
                        filter.setValue(data, forKey: "inputMessage")
                        let transform = CGAffineTransform(scaleX: 7, y: 7)
                        if let output = filter.outputImage?.transformed(by: transform), let cgImage = context.createCGImage(output, from: output.extent) {
                            DispatchQueue.main.async {
                                self.codeImageView.image = UIImage(cgImage: cgImage)
                            }
                        }
                    }
                }
            }).disposed(by: self.disposeBag)
        
        iconImageView.layer.borderWidth = 1
        iconImageView.layer.borderColor = UIColor.white.cgColor
        
        requestData()
        
    }
    
    public class func getImageFromView(view: UIView) -> UIImage {
        // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
        UIGraphicsBeginImageContextWithOptions(view.mj_size, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()
        view.layer.render(in: context!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    func requestData(){
        XS_NetWork.requestJson("/user/queryUserInfoForInvite")
            .mapModel(type: XS_InviteModel.self) // 返回 模型对象 和原始数据
            .subscribe(onNext: { model, resp in
                self.iconImageView.sd_setImage(with: URL.init(string: model?.userImg ?? ""), completed: nil)
                self.nameLabel.text = model?.userName ?? ""
                self.contentLabel.text = String.init(format: "我是%@，我正在学到在线学习，现在赠送你学到币来了哦，邀请你一起学习，快来使用学到币兑换你专属的精品课程吧~\n\n一起晋升，加薪！", model!.userName)
            }).disposed(by: disposeBag)
    }
    
    @IBAction func sessionBtn(_ sender: QMUIButton) {
        let req = SendMessageToWXReq.init()
        let imageObject = WXImageObject()
        imageObject.imageData = XS_InviteVC.getImageFromView(view: snapView).pngData() ?? Data()
        let message = WXMediaMessage()
        message.title = "1122"
        message.description = "2233"
        message.mediaObject = imageObject
        message.messageExt = "3344"
        message.messageAction = "4455"
        message.mediaTagName = "5566"
        message.setThumbImage(addImage)
        req.message = message
        req.scene = Int32(WXSceneSession.rawValue)
        req.bText = false
        WXApi.send(req, completion: nil)
    }
    
    @IBAction func timelineBtnAction(_ sender: QMUIButton) {
        let req = SendMessageToWXReq.init()
        let imageObject = WXImageObject()
        imageObject.imageData = XS_InviteVC.getImageFromView(view: snapView).pngData() ?? Data()
        let message = WXMediaMessage()
        message.title = "1122"
        message.description = "2233"
        message.mediaObject = imageObject
        message.messageExt = "3344"
        message.messageAction = "4455"
        message.mediaTagName = "5566"
        message.setThumbImage(addImage)
        req.message = message
        req.scene = Int32(WXSceneTimeline.rawValue)
        req.bText = false
        WXApi.send(req, completion: nil)
    }
    
    @IBAction func qqBtn(_ sender: QMUIButton) {
        
    }
    
    @IBAction func picBtnAction(_ sender: QMUIButton) {
        let img: UIImage = UIImage.init(data: (XS_InviteVC.getImageFromView(view: snapView).pngData() ?? Data())) ?? addImage
        let shareItems:Array = [img]
        let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
        self.present(activityViewController, animated: true, completion: nil)
    }
}
